package com.perfect

import androidx.room.*
import kotlinx.coroutines.flow.Flow

@Dao
abstract class BrandDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    protected abstract fun insertBrands(data: List<BrandItem>)

    @Query("select * from brands where dataType=:dataType")
    abstract fun getAll(dataType: Byte): List<BrandItem>

    @Delete
    abstract fun delete(data: List<BrandItem>)

    @Query("select * from brands where dataType=:dataType")
    abstract fun getAllFlow(dataType: Byte): Flow<List<BrandItem>>
    /**
     * @param dataType one of [EditBase_DataType_Brands],[EditBase_DataType_BeautySquad]
     * @param data 待插入的数据
     */
    fun insert(dataType: Byte, data: List<BrandItem>) {
        val cacheData = getAll(dataType)
        val brandStyleItems = ArrayList<BrandStyleItem>(512)
        data.forEachIndexed { index, brandItem ->
            brandItem.dataType = dataType
            brandItem.position = index
            if (!brandItem.styleNoList.isNullOrEmpty()) brandStyleItems.addAll(brandItem.styleNoList!!)
        }
        val toBeDelete = data - cacheData
        if (toBeDelete.isNotEmpty()) delete(toBeDelete)
        insertBrands(data)
        DataBaseHolder.getAppDataBase().getBrandStyleDao().insert(brandStyleItems)
    }
}

@Dao
abstract class BrandStyleDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    protected abstract fun insertStyles(data: List<BrandStyleItem>)

    @Query("select * from brand_style_item")
    abstract fun getAll(): List<BrandStyleItem>

    @Query("select * from brand_style_item where  parentId=:brandId order by sort asc")
    abstract fun getByBrandId(brandId: Int): List<BrandStyleItem>

    @Delete
    abstract fun delete(data: List<BrandStyleItem>)

    fun insert(data: List<BrandStyleItem>) {
        val cacheData = getAll()
        val toBeDelete = data - cacheData
        if (toBeDelete.isNotEmpty()) delete(toBeDelete)
        insertStyles(data)

    }
}

@Dao
abstract class ArtistsDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    protected abstract fun insertArtist(data: List<ArtistAccount>)

    fun insertArtists(data: List<ArtistAccount>) {
        val cacheList = getAll()
        val styleList = ArrayList<Styles>(512)
        data.forEachIndexed { index, artistAccount ->
            artistAccount.position = index
            artistAccount.styleNoList?.let { styleList.addAll(it) }

        }

        val deletedList = data - cacheList
        if (deletedList.isNotEmpty()) delete(deletedList)

        insertArtists(data)
        DataBaseHolder.getAppDataBase().getStyleDao().insertStyle(styleList)
    }

    @Query("select * from account_artist order by position asc")
    abstract fun getAll(): List<ArtistAccount>

    @Delete
    abstract fun delete(list: List<ArtistAccount>)


}

@Dao
abstract class StyleDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    protected abstract fun insert(styleNoList: List<Styles>)

    @Query("select * from makeups")
    abstract fun getAll(): List<Styles>

    @Delete
    abstract fun delete(styleNoList: List<Styles>)

    @Query("select * from makeups where accountId=:artistId order by sort asc")
    abstract fun getByArtistId(artistId: String): List<Styles>

    fun insertStyle(styleNoList: List<Styles>) {
        val cache = getAll()
        val toBeDelete = styleNoList - cache
        if (toBeDelete.isNotEmpty()) delete(toBeDelete)
        insert(styleNoList)
    }
}